﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using XRpgLibrary.Events;
using EyesOfTheDragon.Events;
using EyesOfTheDragon.Components;
using Anthem.Components;
using XRpgLibrary.SpriteClasses;

namespace EyesOfTheDragon.Attacks
{
    public class BossAttack : IPhysics, IDraw, IUpdate, IDestroy
    {
        Texture2D texture;
        //boss doesn't coming back.
        private Vector2 position = Vector2.Zero;
        private Vector2 velocity = Vector2.Zero;
        private float lifeTime = 3;
        private AnimatedSprite sprite;

        private bool enabled = false;
        public bool Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }
        public BossAttack(Game gameRef)
        {
            texture = gameRef.Content.Load<Texture2D>(@"Effects\bossFireball");
        }

        public void init()
        {
            PhysicsSimulation.Instance.add(this);
            DrawList.Instance.add(this);
            UpdateList.Instance.add(this);
            enabled = true;
            Dictionary<AnimationKey, Animation> animations1 = new Dictionary<AnimationKey, Animation>();
            Animation animation = new Animation(5, 96, 96, 0, 0);
            animation.Texture = texture;
            animations1.Add(AnimationKey.Right, animation);
            sprite = new AnimatedSprite(texture, animations1);
            sprite.CurrentAnimation = AnimationKey.Right;
            EventRouter.Instance.add(MonsterGenerator.Map_Changing, MapChanging);
            EventRouter.Instance.add(MonsterGenerator.Boss_Killed, MapChanging);
            EventRouter.Instance.add(Player.PlayerKilled, MapChanging);
           
        }
        public void MapChanging()
        {
            DestroyList.Instance.add(this);
        }
        public void destroy()
        {
            PhysicsSimulation.Instance.remove(this);
            DrawList.Instance.remove(this);
            UpdateList.Instance.remove(this);
            EventRouter.Instance.remove(MonsterGenerator.Map_Changing, MapChanging);
            EventRouter.Instance.remove(MonsterGenerator.Boss_Killed, MapChanging);
            EventRouter.Instance.remove(Player.PlayerKilled, MapChanging);
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }
        public void Draw(GameTime gt, SpriteBatch sp)
        {
           
           // Rectangle ourRect = new Rectangle((int)position.X, (int)Position.Y, texture.Width, texture.Height);
            sprite.position = position;
            sprite.Draw(gt, sp);
           // sp.Draw(texture, ourRect, new Rectangle(0, 0, texture.Width, texture.Height), Color.White, rot, new Vector2(texture.Width, texture.Height), SpriteEffects.None, 0);
        }
        public void Update(GameTime gt)
        {
            lifeTime -= gt.ElapsedGameTime.Milliseconds / 1000f;
            if (lifeTime < 0)
            {
                Enabled = false;
                DestroyList.Instance.add(this);
            }
            sprite.Update(gt);
            Rectangle ourRect = new Rectangle((int)position.X, (int)Position.Y, texture.Width, texture.Height);
            Rectangle playerRect = Player.Instance.getRect();
            if (Vector2.Distance(sprite.position, Player.Instance.Sprite.position) > 25)
            return;
            if (playerRect != null)
            {
                if (ourRect.Intersects(playerRect))
                {
                    
                    Player.Instance.Health -= 10;
                    
                    DestroyList.Instance.add(this);
                    return;
                }
            }
           
        }
    }
}

